package com.neuedu.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * http://localhost:8080/web03/login?username=admin&password=123456
 */
@WebServlet(urlPatterns = "/login")
public class LoginController  extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //1 获取用户名、密码

        // null.xxx()
        //2    admin 123456
        /**
         *      req.getSession(true);
         *          true   1 已经存在，获取当前， 2 没有 创建
         *          false  获取当前的（没有  返回null）
         */
        if(
                    (   "admin".equals(username)  || "user".equals(username)  )  &&
                            "123456".equals(password)

        ){
            HttpSession session = req.getSession(); // 1 创建    2  获取


            //设置过期时间  10秒
//            session.setMaxInactiveInterval(10);

            System.out.println("session.getId() = " + session.getId());
            session.setAttribute("username",username);
            session.setAttribute("status",true);
            resp.getWriter().write("登录成功");
            System.out.println("登录成功");
        }else{
            resp.getWriter().write("登录失败");
            System.out.println("登录失败");
        }
         //2.1 记录等状态
        //2.2 失败的情况.......
    }
}
